package common.manager
{
	import common.core.Core;
	import common.core.GSprite;
	import common.core.SDef;
	
	import flash.display.DisplayObjectContainer;
	import flash.events.Event;
	import flash.events.EventDispatcher;
	import flash.events.IEventDispatcher;

	/**
	 * 层管理
	 * @author wanglin
	 * 
	 */	
	public class GLayer extends EventDispatcher
	{
		private static var instance:GLayer;
		private var _currentContain:DisplayObjectContainer;
		
		private var m_keyLayer : GSprite;
		private var m_worldLayer : GSprite ;
		private var m_uiLayer : GSprite;
		private var m_mouseLayer : GSprite;
		
		public function GLayer()
		{
			
		}
		
		public function build(contain:DisplayObjectContainer) : void
		{
			_currentContain = contain;
			
			m_keyLayer = new GSprite;
			m_worldLayer = new GSprite;
			m_uiLayer = new GSprite;
			m_mouseLayer = new GSprite;
			
			_currentContain.addChild(m_keyLayer);
			_currentContain.addChild(m_worldLayer);
			_currentContain.addChild(m_uiLayer);
			_currentContain.addChild(m_mouseLayer);
			
			Core.stgH = m_worldLayer.stage.stageHeight;
			Core.stgW = m_worldLayer.stage.stageWidth;
			
			_currentContain.stage.addEventListener(Event.RESIZE,onRsize);
		}
		
		public function get key() : GSprite
		{
			return m_keyLayer;
		}
		
		public function get world():GSprite
		{
			return m_worldLayer;
		}
		
		public function get ui() : GSprite
		{
			return m_uiLayer;
		}
		
		public function get mouse() : GSprite
		{
			return m_mouseLayer;
		}
		
		public static function  get Instance() : GLayer
		{
			if(instance==null){
				
				instance = new GLayer;
				
			}
			return instance;
		}
		
		public function destory() : void
		{
			_currentContain = null;
			m_worldLayer = null ;
			m_uiLayer = null;
			m_keyLayer = null;
			m_mouseLayer = null;
		}
		
		private function onRsize(e:Event):void{
			GUI.autoAlgin();
			
			Core.stgH = m_worldLayer.stage.stageHeight;
			Core.stgW = m_worldLayer.stage.stageWidth;
			
			ObserverEvt.Send(SDef.WNDRESIZE);
		}
	}
}